package com.hieuzest.citsaw;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Giraffe on 2017/11/25.
 */


public class MyImageView extends View {
    private Bitmap mBitmap;
    private Paint mPaint;
    private Matrix mMatrix;
    private boolean mFlag = true;
    private int mHeight = 1920;
    private int mWidth = 1080;
    public MyImageView(Context context) {
        super(context);
        init();
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.timg);
        Point size = new Point();
        ((Activity)getContext()).getWindowManager().getDefaultDisplay().getSize(size);
        mWidth = size.x;
        mHeight = size.y;
        setMatrix(5, 0);
    }


    public boolean setMatrix(double dist, float yaw) {
        Log.i("IMAGEVIEW", "d " + dist + " y " + yaw);
        Matrix matrix = new Matrix();
        int DIST = 5;
        float MID = (float)(mBitmap.getHeight() * (DIST - dist) * 0.7);
        float kk = 0.4f;
        float rad = yaw / 90;
        if (rad > 3) rad -= 4;
        if (rad < -3) rad += 4;
        if (rad > 1 || rad < -1) return mFlag = false;
        float kd = 0.3f;
        float km = 1;
        float ld = 1 - rad * kd;
        float rd = 1 + rad * kd;
        float H = 800;
        float W = 800;
        float x1 = -W / 2 - MID * rad * 0.7f;
        float x2 = x1;
        float x3 = W / 2 - MID * rad * 0.7f;
        float x4 = x3;
        float y1 = H * ld - MID / 4;
        float y2 = -H * ld + MID / 4;
        float y3 = H * rd - MID / 4;
        float y4 = -H * rd + MID / 4;
        float mW = mWidth / 2;
        float mH = mHeight / 2;
        x1 *= kk / 4;
        y1 *= kk / 4;
        x2 *= kk / 4;
        y2 *= kk / 4;
        x3 *= kk;
        y3 *= kk;
        x4 *= kk;
        y4 *= kk;
        x1 += mW;
        y1 += mH;
        x2 += mW;
        y2 += mH;
        x3 += mW;
        y3 += mH;
        x4 += mW;
        y4 += mH;
        Log.i("IMAGEVIEW", "  " + x1 +" " + y1 + "| " + x2 + " " + y2 + "| " + x3  + " " + y3 + "| " + x4 + " " + y4 + " " + rad);
        matrix.setPolyToPoly(new float[]{0, 0, 0, mBitmap.getHeight(), mBitmap.getWidth(), 0, mBitmap.getWidth(), mBitmap.getHeight()}, 0, new float[]{x1, y1, x2, y2, x3, y3, x4, y4}, 0, 4);
        mMatrix = matrix;
        return mFlag = true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mFlag)
        canvas.drawBitmap(mBitmap, mMatrix, mPaint);
    }

}
